<?php

namespace app\api\controller\paotui;

use addons\paotui\library\Map;
use app\common\controller\Api;
use think\Validate;


/**
 * 会员地址接口
 */
class UserAddress extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 最后一次的发件地址
     */
    public function last_from_address(){
        $addressService = new \addons\paotui\service\UserAddress();
        $addressInfo = $addressService->getLastFromAddress($this->auth->id);
        $this->success("请求成功",$addressInfo);
    }

    /**
     * 地址列表
     */
    public function index(){
        $page = $this->request->post('page',0);
        $limit = $this->request->post('limit',0);
        $keyword = addslashes($this->request->post("keyword",''));

        $where['user_id'] = $this->auth->id;
        $model = new \addons\paotui\service\UserAddress();
        $data = $model->getUserAddressList($where,$page,$limit,"*","lasttime desc",$keyword);

        $this->success('请求成功',$data);
    }

    /**
     * 地址详情
     */
    public function info(){
        $id = $this->request->post("id",0);     //1同城 2全国

        $model = new \addons\paotui\service\UserAddress();
        $data = $model->getAddressInfo($id,$this->auth->id);

        $this->success('请求成功',$data);
    }


    /**
     * 添加地址
     */
    public function add(){

        $data['lng'] = $this->request->post("lng");
        $data['lat'] = $this->request->post("lat");
        $data['poi'] = $this->request->post("poi");
        $data['address'] = $this->request->post("address");
        $data['name'] = $this->request->post("name");
        $data['mobile'] = $this->request->post("mobile");
        $data['lasttime'] = time();

        $map = new Map();
        $area = $map->getCity($data['lng'] ,$data['lat']);
        if(empty($area)){
            $this->error("位置获取失败，请重新选择地址！");
        }

        if(!\addons\paotui\service\UserAddress::checkCity($area['city'])){
            $this->error("当前选择位置不在运营范围内！");
        }

        $data['province'] = $area['province'];
        $data['city'] = $area['city'];
        $data['district'] = $area['district'];
        $data['user_id'] = $this->auth->id;

        $msg = [
            'lng.require' => '请选择精确地址',
            'lat.require' => '请选择精确地址',
            'poi.require' => '请选择精确地址',
            'name.require' => '姓名不能为空',
            'mobile.require' => '手机号不能为空',
            'mobile.length' => '手机号格式不正确',
            'address.require' => '详细地址不能为空'
        ];

        $rule = [
            'lng'  => 'require',
            'lat'  => 'require',
            'poi'=>'require',
            'name'=>'require',
            'mobile'=>'require|length:11',
            'address'=>'require',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($data);
        if (!$result) {
            $this->error(__($validate->getError()), null,[]);
        }

        $model = new \app\admin\model\paotui\UserAddress();
        if($model->allowField(true)->save($data)){
            $this->success('添加成功',$model);
        }else{
            $this->error('添加失败');
        }
    }

    /**
     * 修改地址
     */
    public function edit(){
        $id = $this->request->post("id");
        $userAddress = new \app\admin\model\paotui\UserAddress();
        $userAddressInfo = $userAddress->where(['id'=>$id,'user_id'=>$this->auth->id])->find();
        if(empty($userAddressInfo)){
            $this->error('当前地址不存在');
        }

        $data['lng'] = $this->request->post("lng");
        $data['lat'] = $this->request->post("lat");
        $data['poi'] = $this->request->post("poi");
        $data['address'] = $this->request->post("address");
        $data['name'] = $this->request->post("name");
        $data['mobile'] = $this->request->post("mobile");
        $data['lasttime'] = time();

        $map = new Map();
        $area = $map->getCity($data['lng'] ,$data['lat']);
        if(empty($area)){
            $this->error("位置获取失败，请重新选择地址！");
        }

        if(!\addons\paotui\service\UserAddress::checkCity($area['city'])){
            $this->error("当前选择位置不在运营范围内！");
        }

        $data['province'] = $area['province'];
        $data['city'] = $area['city'];
        $data['district'] = $area['district'];

        $msg = [
            'lng.require' => '请选择精确地址',
            'lat.require' => '请选择精确地址',
            'poi.require' => '请选择精确地址',
            'name.require' => '姓名不能为空',
            'mobile.require' => '手机号不能为空',
            'mobile.length' => '手机号格式不正确',
            'address.require' => '详细地址不能为空'
        ];

        $rule = [
            'lng'  => 'require',
            'lat'  => 'require',
            'poi'=>'require',
            'name'=>'require',
            'mobile'=>'require|length:11',
            'address'=>'require',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($data);
        if (!$result) {
            $this->error(__($validate->getError()), null,[]);
        }

        if($userAddressInfo->save($data)){
            $this->success('编辑成功',$userAddressInfo);
        }else{
            $this->error('编辑失败');
        }
    }

    /**
     * 删除
     */
    public function del(){
        $id = $this->request->post("id");
        $userAddress = new \app\admin\model\paotui\UserAddress();
        $userAddressInfo = $userAddress->where(['id'=>$id,'user_id'=>$this->auth->id])->find();
        if(empty($userAddressInfo)){
            $this->error('当前地址不存在');
        }
        $userAddressInfo->delete();
        $this->success('请求成功');

    }

}
